//
//  UICustomImageView.m
//  takeout
//
//  Created by Derekc on 12-3-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UICustomImageView.h"
#import "PublicFunction.h"
#import "Constants.h"

@implementation UICustomImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) setImageURL:(NSString* )imgUrl{
    if (imgUrl == nil ||[imgUrl length] ==0) {
        [self setImage:nil];
        return;
    }
    
    if ([PublicFunction isExistImage:imgUrl]) {
        [self setImage:[PublicFunction imageWithName:[PublicFunction getLocalImageName:imgUrl]]];
    }
    else{
        UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [act setFrame:CGRectMake(floor(self.center.x-10), floor(self.center.y-10), 21, 21)];
        [act startAnimating];
        [self addSubview:act];
        
        NSMutableDictionary *obj = [[NSMutableDictionary alloc] init];
        [obj addEntriesFromDictionary:[NSDictionary dictionaryWithObject:imgUrl forKey:@"URL"]];
        [obj addEntriesFromDictionary:[NSDictionary dictionaryWithObject:act forKey:@"ACT"]];
        NSOperationQueue *threadQueue = [[NSOperationQueue alloc] init];
        [threadQueue setMaxConcurrentOperationCount:kMaxThreadNumber];
        NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self
                                                                         selector:@selector(updateImage:)
                                                                           object:obj];
        [threadQueue addOperation:op];
    }
}

- (void)updateImage:(NSMutableDictionary *)obj
{
    NSString* url = [obj objectForKey:@"URL"];
    UIActivityIndicatorView *t_act = [obj objectForKey:@"ACT"];
	NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url] ];
	if(data)
    {
		UIImage *image = [UIImage imageWithData:data];
        
        if(image)
        {
            if (t_act != nil){
                [t_act stopAnimating];
                t_act.hidden = YES;
                [t_act removeFromSuperview];
            }
            [self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
            
            NSArray* pathSplit = [url componentsSeparatedByString:@"/"];
            NSString* name = [pathSplit lastObject];
            [PublicFunction saveImageData:data withName:name];
        }
        else
        {
            CUSTOM_LOG(url);
        }
        
		image = nil;
		data = nil;
	}
    else
    {
        CUSTOM_LOG(url);
    }
	[[NSURLCache sharedURLCache] setMemoryCapacity:0];
	[[NSURLCache sharedURLCache] setDiskCapacity:0];
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
